<?php

/**
 * @TODO refactor everything
 * Enter description here ...
 * @author asilis
 *
 */
class asImage {
	protected $filename;
	protected $mime;
	protected $adapter;

	const ERROR_IMAGE_DOESNT_EXISTS=4001;
	const ERROR_UNABLE_TO_CONSTRUCT_SFIMAGE=4002;

	public function __construct($filename="", $mime="", $adapter=""){
		if (!file_exists($filename)){
			//throw new sfException("Image file doesnt exists", self::ERROR_IMAGE_DOESNT_EXISTS);
			return false;
		}

		$this->filename=$filename;
		$this->mime=$mime;
		$this->adapter=$adapter;
	}

	public function __toString(){
		return $this->getUrl();
	}

	public function getUrl(){
		return $this->getPublicPath();
	}


	/**
	 *
	 * @return sfImage
	 */
	private function constructSfImage(){
		if (empty($this->mime)){
			if (class_exists("stFileUtil")){
				$this->mime=stFileUtil::getMimeTypeForFile($this->filename);
			}
		}
		$sfImage=new sfImage($this->filename, $this->mime, $this->adapter);
		if (!($sfImage instanceOf sfImage)){
			throw new sfException("Unable to construct sfImage instance", self::ERROR_UNABLE_TO_CONSTRUCT_SFIMAGE);
		}

		return $sfImage;
	}

	public function getFilename(){
		return $this->filename;
	}

	public function getPublicPath(){
		return PathHelper::getPublicPath($this->getFilename());
	}


	private function getDynamicFilePathWoExtension($methodSuffix, $params){
		$methodSuffix=str_replace("get", "", strtolower($methodSuffix));
		$methodSuffix=$methodSuffix."-".implode("-", $params);

		$dynDirPath="";

		$fName=trim($this->getFilename());
		if (!empty($fName)){
			$orImgPathInfo=pathinfo($this->getFilename());
			$dynDirPath=$orImgPathInfo["dirname"]."/".$orImgPathInfo["filename"]."-".$methodSuffix;
		}

		# izveidojam folderi, ja tas neeksistē
/*		if (!file_exists($dynDirPath)){
			mkdir($dynDirPath, 0775, true);
		}*/

		return $dynDirPath;
	}

	/**
	 *
	 * @param $width
	 * @param $height
	 * @param $top
	 * @param $left
	 * @return stSfImage
	 */
	public function getCropped($width=null, $height=null, $left=null, $top=null){
		if ($width === null && $height === null)
			return $this;

		$funcArgs=func_get_args();
		$dynPath=$this->getDynamicFilePathWoExtension(__FUNCTION__, $funcArgs);
		//ja thumb neeksistee, uzgjenereejam
		$newFilePath=$dynPath.".jpg";

		if (file_exists($newFilePath)){
			return new asImage($newFilePath, "image/jpg");
		}

		try {
			$img=$this->constructSfImage();
			$img->setQuality(90);
			$img->crop($left, $top, $width, $height);
			$img->saveAs($newFilePath, "image/jpg");
			return new asImage($newFilePath, "image/jpg");
		}catch(Exception $e){
			return false;
		}
	}

	/**
	 *
	 * @param $width
	 * @param $height
	 * @return StSfImage
	 */
	public function getResized($width=null, $height=null, $inflate=true, $proportional=false){
		if ($width === null && $height === null)
			return $this;

		$funcArgs=func_get_args();
		$dynPath=$this->getDynamicFilePathWoExtension(__FUNCTION__, $funcArgs);
		//ja thumb neeksistee, uzgjenereejam
		$newFilePath=$dynPath.".jpg";
		if (file_exists($newFilePath)){
			return new asImage($newFilePath, "image/jpg");
		}

		try {
			/** @TODO: check zis aut **/
//			if (!empty($width) && !empty($height)){
//				$proportional=false;
//			}

			$img=$this->constructSfImage();
			$img->setQuality(90);
			$img->resize($width, $height, $inflate, $proportional);
			$img->saveAs($newFilePath, "image/jpg");
			return new asImage($newFilePath, "image/jpg");
		}catch(Exception $e){
			return false;
		}
	}

	public function getThumbnail($width=null, $height=null, $position="center"){
		if ($width === null && $height === null)
			return $this;

		$funcArgs=func_get_args();
		$dynPath=$this->getDynamicFilePathWoExtension(__FUNCTION__, $funcArgs);
		//ja thumb neeksistee, uzgjenereejam
		$newFilePath=$dynPath.".jpg";
		if (file_exists($newFilePath)){
			return new asImage($newFilePath, "image/jpg");
		}

		try {
			$img=$this->constructSfImage();
			$img->setQuality(90);
			$img->thumbnail($width, $height, $position);
			$img->saveAs($newFilePath, "image/jpg");
			return new asImage($newFilePath, "image/jpg");
		}catch(Exception $e){
			return false;
		}
	}


	/**
	 *
	 * @param $width
	 * @param $height
	 * @return stSfImage
	 */
	public function getCenteredCut($width=null, $height=null){
		if ($width === null && $height === null)
			return $this;

		$img	=	$this->constructSfImage();
		$left	=	intval(($img->getWidth())/2)-intval($width/2);
		$top	=	intval(($img->getHeight())/2)-intval($height/2);

		try {
			return $this->getCropped($width, $height, $left, $top);
		}catch(Exception $e){
			return false;
		}
	}


	public function __call($methodName, $arguments){
		if (!method_exists("sfImage", $methodName)){
			return false;
		}

		$sfImage=$this->constructSfImage();
		if (!($sfImage instanceOf sfImage)){
			return false;
		}

		$res=call_user_func_array(array($sfImage, $methodName), $arguments);
		return $res;
	}


}


